package com.dofast.bt.mvp.view.activity.basket;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.presenter.basket.PaymentPresenter;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;
import com.dofast.bt.widget.MyListView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * username:Created by hsj on 2016/12/18 0018.
 * class：
 * 类描述：确认付款界面
 */

public class PaymentActivity extends BaseMvpActivity<PaymentPresenter, PaymentView> implements PaymentView {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_all_money)
    TextView tvAllMoney;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ll_all)
    LinearLayout llAll;
    @BindView(R.id.iv_address)
    ImageView ivAddress;
    @BindView(R.id.iv_selcect)
    ImageView ivSelcect;
    @BindView(R.id.tv_name_s)
    TextView tvNameS;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_address_s)
    TextView tvAddressS;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.lv_goods)
    MyListView lvGoods;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_payment;
    }

    @Override
    protected void initView() {
        initTitle();
        presenter.initData();
        ivSelcect.setVisibility(View.INVISIBLE);
        ivAddress.setVisibility(View.VISIBLE);
        llAddress.setBackgroundColor(getResources().getColor(R.color.bg_bottom));
    }


    @Override
    protected void addOnClickListener() {

    }

    @Override
    public Loader<PaymentPresenter> onCreateLoader(int id, Bundle args) {
        return new PresenterLoder<>(this, new PresenterFactory<PaymentPresenter>() {
            @Override
            public PaymentPresenter create() {
                return new PaymentPresenter();
            }
        });
    }

    @Override
    public void initAdapter(Adapter adapter) {
        lvGoods.setAdapter((ListAdapter) adapter);

    }

    @Override
    public void showDiaolog() {

    }

    /**
     * @param context 啓動界面
     * @param address 地址
     * @param goods   待付款商品清單
     */
    public static void gotoPaymentActivity(Context context, AddressBean address, List<ShoppingCartBean> goods) {
        Intent intent = new Intent(context, PaymentActivity.class);
        intent.putExtra("addess", address);
        intent.putExtra("goods", (Serializable) goods);
        context.startActivity(intent);
    }

    @Override
    public AddressBean getAddress() {
        return (AddressBean) getIntent().getSerializableExtra("addess");
    }

    @Override
    public List<ShoppingCartBean> getGoods() {
        return (List<ShoppingCartBean>) getIntent().getSerializableExtra("goods");
    }

    @Override
    public void setAddress(AddressBean address) {
        tvName.setText(address.getName());
        tvPhone.setText(address.getPhone());
        tvAddress.setText(address.getAddress());
    }

    @Override
    public void setAllMoney(String s) {
        tvAllMoney.setText(s);
    }

    @Override
    public void initTitle() {
        tvTitle.setText("确认付款");
        tvEdit.setVisibility(View.GONE);
        ivBack.setVisibility(View.VISIBLE);
    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_commit:
                // TODO: 2016/12/18 0018确认付款逻辑处理
                presenter.pay();
                break;
        }
    }
}
